package huoye.shops.huoye.mine.presenter;


import android.os.CountDownTimer;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.codeideology.android.rpc.BaseJsonRequestCallback;

import java.util.List;

import huoye.shops.MainActivity;
import huoye.shops.R;
import huoye.shops.base.BasePresenter;
import huoye.shops.base.MineBaseJsonRequestCallback;
import huoye.shops.base.UserManager;
import huoye.shops.common.model.SMSCoder;
import huoye.shops.huoye.mine.model.AddAccountInfo;
import huoye.shops.huoye.mine.model.EditAccountInfo;
import huoye.shops.huoye.mine.model.ShopDetials;
import huoye.shops.huoye.mine.networt.MineService;
import huoye.shops.huoye.mine.view.IEditAccoundView;
import huoye.shops.huoye.user.model.UpdateInfo;
import huoye.shops.huoye.user.model.User;
import huoye.shops.huoye.user.network.UserService;
import huoye.shops.huoye.user.presenter.LoginPresenter;
import huoye.shops.huoye.user.view.ILoginView;


/**
 * Created by uesr on 2017/7/19.
 */

public class EditAccoundPresenter extends BasePresenter<IEditAccoundView> {


    private MineService mService;
    private MyCountDownTimer myCountDownTimer;


    //发送验证码
    public void senCode(String userTel){
        mService.sendRestPasswordSms(userTel,new MineBaseJsonRequestCallback<SMSCoder>(SMSCoder.class) {

            @Override
            public void onSuccess(SMSCoder smsCoder) {
                super.onSuccess(smsCoder);
                view.makeToast(R.string.login_code_confirm);
                myCountDownTimer.start();
                view.getCodeData(smsCoder);
            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);
                //   Log.e("testaaa",msg);
            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });
    }

    //新增时获取账户权限列表  (获取微信二维码)
    public void addAccountInfo(){
        mService.addAccountInfo( new MineBaseJsonRequestCallback<AddAccountInfo>(AddAccountInfo.class) {

            @Override
            public void onSuccess(AddAccountInfo addAccountInfo) {
                super.onSuccess(addAccountInfo);
                view.getCodeStr(addAccountInfo);
            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);

            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });
    }


    //新增时获取账户权限列表  (获取微信二维码)
    public void editAccountInfo(String editUserId){
        mService.editAccountInfo( editUserId,new MineBaseJsonRequestCallback<EditAccountInfo>(EditAccountInfo.class) {

            @Override
            public void onSuccess(EditAccountInfo editAccountInfo) {
                super.onSuccess(editAccountInfo);
                view.getEditAccountInfo(editAccountInfo);
            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);

            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });
    }




    //保存账户（新增或编辑）
    public void saveAccount(String saveUserId,String accountName, String powerStr, String accountState,boolean isEdit,String codeId,String code,String mobile){
        mService.saveAccount(saveUserId,accountName,powerStr,accountState,codeId,code,mobile, new MineBaseJsonRequestCallback<AddAccountInfo>(AddAccountInfo.class) {

            @Override
            public void onSuccess(AddAccountInfo addAccountInfo) {
                super.onSuccess(addAccountInfo);
                if(isEdit){
                    view.makeToast("编辑商家管理账户成功！");
                }else {
                    view.makeToast("保存商家管理账户成功！");
                }
                view.finish();

            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);

            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });
    }



    @Override
    public void onStart() {
        mService = new MineService();
        //new倒计时对象,总共的时间,每隔多少秒更新一次时间
        myCountDownTimer = new MyCountDownTimer(60000,1000);
    }

    //复写倒计时
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            view.codeByoyomi("("+l/1000+")",true);

        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            view.codeByoyomi("获取验证码",false);
        }
    }

}
